<?php


namespace app\api\model\Video;


use think\facade\Db;
use think\Model;

class VideoModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "video";

    /**
     * 根据条件查询单条数据
     * @param $where
     * @param string $field
     * @return mixed
     * @throws
     */
    public static function getVideoWhere($where, $field = "*")
    {
        $video = Db::name("video")->field($field)->where($where)->find();
        return $video;
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @param $field
     * @return mixed
     * @throws
     */
    public static function getVideoWhereMore($where, $field = "*")
    {
        $list = Db::name("video")->field($field)->where($where)->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getVideoCountWhere($where)
    {
        $count = Db::name("video")
            ->alias('v')
            ->join('video_cate_relation vcr', 'vcr.video_id = v.id', 'left')
            ->where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getVideoListWhere($page, $limit, $where = [], $field = "v.*")
    {
        $start = ceil(($page - 1) * $limit);
        $list = Db::name('video')
            ->alias('v')
            ->join('video_cate_relation vcr', 'vcr.video_id = v.id', 'left')
            ->field($field)
            ->where($where)
            ->limit($start, $limit)
            ->order('v.sort desc')
            ->select()->toArray();
        return $list;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getVideoListWheres($page, $limit, $uid, $where = [], $field = "v.*")
    {
        $start = ceil(($page - 1) * $limit);
        $list = Db::name('video')
            ->alias('v')
            ->join('video_cate_relation vcr', 'vcr.video_id = v.id', 'left')
            ->join('video_user_relation re', 'v.id = re.video_id', 'left')
            ->field($field)
            ->where($where)
            ->where('re.user_id', '=', $uid)
            ->limit($start, $limit)
            ->order('v.sort desc')
            ->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getVideoCountWheres($where, $uid)
    {
        $count = Db::name("video")
            ->alias('v')
            ->join('video_cate_relation vcr', 'vcr.video_id = v.id', 'left')
            ->join('video_user_relation re', 'v.id = re.video_id', 'left')
            ->where('re.user_id', '=', $uid)
            ->where($where)->count();
        return $count;
    }
}